package com.fiveflavors.suittar.gui.utils;

import java.net.URL;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.fiveflavors.suittar.SuittarCommand;

/**
 * 文字列置換ユーティリティ
 * @author marr
 */
public class Converter {
	
	/**
	 * UTC String値"yyyyMMddHHmmss"から、JST long値 への変換
	 * @param utcString UTCの"yyyyMMddHHmmss"表記
	 * @return long値
	 */
	public static final long convertUTCString2JSTLong(String utcString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return Long.parseLong(sdf.format(convertUTCString2JSTDate(utcString)));
	}

	/**
	 * UTC String"yyyy/MM/dd/ HH:mm:ss"値から、JST
	 * @param utcString UTCの"yyyy/MM/dd/ HH:mm:ss"表記
	 * @return
	 */
	public static final String convertUTCString2JSTString(String utcString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return sdf.format(convertUTCString2JSTDate(utcString));
	}

	/**
	 * UTCの文字列"EEE MMM dd HH:mm:ss Z yyyy"からJST {@link Date Date} への変換
	 * @param utcString "EEE MMM dd HH:mm:ss Z yyyy"
	 * @return {@link Date Date}
	 */
	public static final Date convertUTCString2JSTDate(String utcString) {

		//Parse DateTime String 2 Date
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",Locale.UK);
		Date utcDt = sdf.parse(utcString,new ParsePosition(0));			
		
		//set UTC Time
		GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
		calendar.setTimeInMillis(utcDt.getTime());

		//to LocalTime
		calendar.setTimeZone(TimeZone.getDefault());

		return calendar.getTime();
	}

	public static final String convertJSTString2UTCString(String jstString) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy",Locale.UK);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        return sdf.format(convertJSTString2UTCDate(jstString));
	}

	public static final Date convertJSTString2UTCDate(String jstString) {
		//Parse DateTime String 2 Date
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.JAPAN);
		Date jstDt = sdf.parse(jstString,new ParsePosition(0));		
		return jstDt;
	}

	public static final String convertURL2ImgTagHtml(URL url) {
		String urlStr = "<IMG SRC=\"" + url.toString() +  "\" BORDER=\"0\">";
		return urlStr;
	}

	/**
	 * Text中の @foobar 及び hyper linkと思しき文字列が有った場合は anchorタグ を追加。
	 * @param values 
	 * @return anchorタグ追加済みの文字列
	 */
	public static final String addAnchorTagInText(String values) {

		if (values == null || values.length() < 1) {
			return "";
		}
		
		//先にhttp(https)にリンクを付与
		values = values.replaceAll("(http://|https://){1}[\\w\\.\\-/:\\?=&]+","<A HREF=\"$0\">$0</A>");

		//@user
		Pattern ptn = Pattern.compile("@[\\w\\-]+",Pattern.CASE_INSENSITIVE);
		Matcher mch = ptn.matcher(values);
		while(mch.find()){
			String before = mch.group();
			String id = before.replaceAll("@", "");
			StringBuffer replacer = new StringBuffer();
			replacer.append("<A HREF=\"http://suittar20/command?command=" + SuittarCommand.COMMAND_SHOW_PROFILE + "&id=" + id + "\">");
			replacer.append(before);
			replacer.append("</A>");
			values = values.replaceAll(before,replacer.toString());
		}

		return values;

	}

}
